package main

import (
	"fmt"
	"logstash/config"
	"logstash/logger"
	"logstash/mq"
	"logstash/mq/queue"
	"logstash/utile"
	"os"
	"os/signal"
)

func main() {

	//配置文件路径
	configFile := "yml/config.yml"
	//加载本地配置，请执行导入配置操作： go-bindata -o=data/data.go -pkg=data yml/...
	conf, err := config.InitConfig(configFile)
	if err != nil {
		panic("初始化项目配置失败,错误信息：" + err.Error())
	}
	//初始化日志配置
	logger.InitConf(conf.Data.App.Log)
	//创建ETCD
	etcd, err := utile.NewEtcd()
	if err != nil {
		panic("Etcd启动失败,错误信息：" + err.Error())
	}
	err = etcd.InitProjectConfig(conf.Data)
	if err != nil {
		panic(err)
	}

	//flag := true
	//
	//if flag {

	rmq := mq.NewMqManager()

	//批量注册生产者
	rmq.RegisterProducers(map[string]mq.Producer{
		queue.PRODUCER_TEST:      &mq.RmqProducer{QueueName: queue.PRODUCER_TEST, RoutingKey: queue.PRODUCER_TEST, ExchangeName: "", ExchangeType: "topic"},           //测试生产者test
		queue.PRODUCER_USER:      &mq.RmqProducer{QueueName: queue.PRODUCER_USER, RoutingKey: queue.PRODUCER_USER, ExchangeName: "", ExchangeType: "topic"},           //测试生产者user
		queue.PRODUCER_HEARTBEAT: &mq.RmqProducer{QueueName: queue.PRODUCER_HEARTBEAT, RoutingKey: queue.PRODUCER_HEARTBEAT, ExchangeName: "", ExchangeType: "topic"}, //心跳检测
	})
	//只有server才需要进行消费者的注册
	//if strings.EqualFold(conf.Data.App.Role, "server") {
	//批量注册消费者
	rmq.RegisterConsumers(map[string]mq.Consumer{
		queue.CONSUMER_TEST:      &mq.RmqConsumer{QueueName: queue.CONSUMER_TEST, RoutingKey: queue.CONSUMER_TEST, ExchangeName: "", ExchangeType: "topic"},           //测试消费者test
		queue.CONSUMER_USER:      &mq.RmqConsumer{QueueName: queue.CONSUMER_USER, RoutingKey: queue.CONSUMER_USER, ExchangeName: "", ExchangeType: "topic"},           //测试消费者user
		queue.CONSUMER_HEARTBEAT: &mq.RmqConsumer{QueueName: queue.CONSUMER_HEARTBEAT, RoutingKey: queue.PRODUCER_HEARTBEAT, ExchangeName: "", ExchangeType: "topic"}, //心跳检测
	})
	//启动消费程序
	rmq.Start()

	//MQ心跳检测
	go mq.GetMqManager().HeartBeat()
	//ES心跳检测
	go utile.GetES().HeartBeat()

	//} else {
	//	//启动监控程序
	Run()
	//}

	//}
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt)

	select {
	case sig := <-c:
		fmt.Printf("Got %s signal. Aborting...\n", sig)
	}
}
